<template>
  <a-spin :spinning='confirmLoading'>
    <j-form-container :disabled='formDisabled'>
      <a-form-model ref='form' :model='model' :rules='validatorRules' slot='detail'>
        <a-row>
          <a-col :span='24'>
            <a-form-model-item label='学年编码' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolYearCode'>
              <a-input v-model='model.schoolYearCode' placeholder='请输入学年名称'></a-input>
<!--              <j-dict-select-tag label="学年编码" type="list" v-model="model.schoolYearCode" dictCode="" placeholder="请选择学年编码"-->
<!--                                 dictCode="cou_gradesession_year,gradesession_year_code,gradesession_year_code,iz_open=1 and del_flag=0 order by gradesession_year_code desc" />-->
            </a-form-model-item>
          </a-col>
          <a-col :span='24'>
            <a-form-model-item label='学年名称' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolYearName'>
              <a-input v-model='model.schoolYearName' placeholder='请输入学年名称'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='24'>
            <a-form-model-item label='学年简称' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='schoolYearNickname'>
              <a-input v-model='model.schoolYearNickname' placeholder='请输入学年简称'></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span='24'>
            <a-form-model-item label='是否默认' :labelCol='labelCol' :wrapperCol='wrapperCol' prop='isDefault'>
              <a-select v-model='model.isDefault' placeholder='请选择是否默认' @change='checkValue'>
                <a-select-option :value=item.value v-for='item in (isDefaultValue)'>{{ item.text }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'CouSchoolYearForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      isDefaultValue: [
        { value: '1', text: '默认' },
        { value: '0', text: '常规' }
      ],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        schoolYearCode: [
          { required: true, message: '请输入学年编码' }
        ],
        schoolYearName: [
          { required: true, message: '请输入学年名称' }
        ],
        isDefault: [
          { required: true, message: '请选择是否默认' }
        ]
      },
      url: {
        add: '/schoolyear/couSchoolYear/add',
        edit: '/schoolyear/couSchoolYear/edit',
        queryById: '/schoolyear/couSchoolYear/queryById',
        queryIsDefault: '/schoolyear/couSchoolYear/queryIsDefault'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  mounted() {
  },
  methods: {
    checkValue() {
      if (this.model.isDefault === '1') {
        let params = { isDefault: this.model.isDefault }
        getAction(this.url.queryIsDefault, params).then((res) => {
          if (res.success) {
            res.result[0].count
            if (res.result[0].count > 0) {
              this.$message.error('已经存在默认选项的学年信息，请确认后在设置该学年是否为默认')
              this.model.isDefault = ''
            }
          } else {
            this.$message.error(res.message)
            this.model.isDefault = ''
          }
        })
      }
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>